package com.felight.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletConfig
 */
public class ServletConf extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletConf() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void init(){
    	System.out.println("server started..");
    }
	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			PrintWriter out = response.getWriter();
			ServletConfig sc = getServletConfig();
			String driver = sc.getInitParameter("driver");
			String url = sc.getInitParameter("url");
			String uname = sc.getInitParameter("username");
			String pwd = sc.getInitParameter("password");
			String table = sc.getInitParameter("tablename");
			out.println("table");
			Class.forName(driver);
			out.println("registration successfull..");
			Connection conn = DriverManager.getConnection(url,uname,pwd);
			out.println("Connection eshtablished");
			Statement stm = conn.createStatement();
			ResultSet rs = stm.executeQuery("select * from "+ table);
			out.println("inside table.");
			while(rs.next()){
				out.println("<h2>"+rs.getInt(1)+"</h2>");
				out.println("<h2>"+rs.getString(2)+"</h2>");
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

}
